﻿using System;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DataGridViewAutoFilter;
using System.Data.SqlServerCe;

namespace LogAnalysis {
	public partial class Form1 : Form {
		


		public Form1() {
			InitializeComponent();
			var encodings = Encoding.GetEncodings();
			Array.Sort<EncodingInfo>(encodings, (x, y) => x.Name.CompareTo(y.Name));
			comboBox1.DataSource = encodings;
			comboBox1.DisplayMember = "DisplayName";
			comboBox1.ValueMember = "Name";
			comboBox1.SelectedValue = Encoding.Default.HeaderName;
			dataGridView1.DataSource = bindingSource1;
		}

		private void button1_Click(object sender, EventArgs e) {
			openFileDialog1.Title = "请选择日志文件";
			if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
				textBox1.Text = openFileDialog1.FileName;
			}
		}

		private void button2_Click(object sender, EventArgs e) {
			if (File.Exists(textBox1.Text)) {
				DataTable tmpData = Utility.ReadFileToTable(textBox1.Text, comboBox1.SelectedValue.ToString());
				
				bindingSource1.DataSource = tmpData;
				bindingSource1.ResetBindings(true);
			}
		}
		private void dataGridView1_BindingContextChanged(object sender, EventArgs e) {
			// Continue only if the data source has been set.
			if (dataGridView1.DataSource == null) {
				return;
			}

			// Add the AutoFilter header cell to each column.
			foreach (DataGridViewColumn col in dataGridView1.Columns) {
				col.HeaderCell = new
					DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
			}

			// Format the OrderTotal column as currency. 
			//dataGridView1.Columns["OrderTotal"].DefaultCellStyle.Format = "c";

			// Resize the columns to fit their contents.
			//dataGridView1.AutoResizeColumns();
		}

		// Displays the drop-down list when the user presses 
		// ALT+DOWN ARROW or ALT+UP ARROW.
		private void dataGridView1_KeyDown(object sender, KeyEventArgs e) {
			if (e.Alt && (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)) {
				DataGridViewAutoFilterColumnHeaderCell filterCell =
					dataGridView1.CurrentCell.OwningColumn.HeaderCell as
					DataGridViewAutoFilterColumnHeaderCell;
				if (filterCell != null) {
					filterCell.ShowDropDownList();
					e.Handled = true;
				}
			}
		}

		// Updates the filter status label. 
		private void dataGridView1_DataBindingComplete(object sender,
			DataGridViewBindingCompleteEventArgs e) {
			String filterStatus = DataGridViewAutoFilterColumnHeaderCell
				.GetFilterStatus(dataGridView1);
			if (String.IsNullOrEmpty(filterStatus)) {
				showAllLabel.Visible = false;
				filterStatusLabel.Visible = false;
			}
			else {
				showAllLabel.Visible = true;
				filterStatusLabel.Visible = true;
				filterStatusLabel.Text = filterStatus;
			}
		}

		// Clears the filter when the user clicks the "Show All" link
		// or presses ALT+A. 
		private void showAllLabel_Click(object sender, EventArgs e) {
			DataGridViewAutoFilterColumnHeaderCell.RemoveFilter(dataGridView1);
		}

		private void button3_Click(object sender, EventArgs e) {
			bindingSource1.Filter = textBox2.Text;
		}
	}

}
